<!DOCTYPE html>
<html>
<head>
    <title>Physics Diagram of a Hemisphere in a Magnetic Field</title>
</head>
<body>

<canvas id="physics_diagram" width="450" height="450" style="border:1px solid #d3d3d3;"></canvas>

<script>
    const canvas = document.getElementById('physics_diagram');
    const ctx = canvas.getContext('2d');

    // --- Configuration ---
    const cx = 225; // Center X of the hemisphere base
    const cy = 200; // Center Y of the hemisphere base
    const hemisphereRadiusX = 150; // Horizontal radius of the base ellipse
    const hemisphereRadiusY = 50;  // Vertical radius of the base ellipse for perspective
    const vectorLength = 120; // Length of the vectors
    const angleAlpha = Math.PI / 5; // The angle α, approximately 36 degrees for visual representation

    // --- General Style ---
    ctx.strokeStyle = '#000000';
    ctx.fillStyle = '#000000';
    ctx.lineWidth = 1.5;
    ctx.textAlign = 'center';
    ctx.textBaseline = 'middle';

    // --- Helper function to draw a vector (line with arrowhead) ---
    function drawVector(fromX, fromY, toX, toY) {
        const headlen = 12; // length of head in pixels
        const angle = Math.atan2(toY - fromY, toX - fromX);
        
        // Main line of the vector
        ctx.beginPath();
        ctx.moveTo(fromX, fromY);
        ctx.lineTo(toX, toY);
        ctx.stroke();

        // Arrowhead
        ctx.beginPath();
        ctx.moveTo(toX, toY);
        ctx.lineTo(toX - headlen * Math.cos(angle - Math.PI / 6), toY - headlen * Math.sin(angle - Math.PI / 6));
        ctx.moveTo(toX, toY);
        ctx.lineTo(toX - headlen * Math.cos(angle + Math.PI / 6), toY - headlen * Math.sin(angle + Math.PI / 6));
        ctx.stroke();
    }

    // 1. Draw the Hemisphere
    // The base S' is an ellipse for perspective
    ctx.beginPath();
    ctx.ellipse(cx, cy, hemisphereRadiusX, hemisphereRadiusY, 0, 0, 2 * Math.PI);
    ctx.stroke();

    // The curved top S is a semi-circle arc
    ctx.beginPath();
    ctx.arc(cx, cy, hemisphereRadiusX, Math.PI, 2 * Math.PI, false);
    ctx.stroke();

    // 2. Draw the Vectors originating from the center of the base
    const originX = cx;
    const originY = cy;

    // Vector e_n (normal vector, pointing straight down)
    const en_endX = originX;
    const en_endY = originY + vectorLength;
    drawVector(originX, originY, en_endX, en_endY);

    // Vector B (magnetic field vector)
    // The angle α is between the vertical vector e_n and B.
    const b_endX = originX + vectorLength * Math.sin(angleAlpha);
    const b_endY = originY + vectorLength * Math.cos(angleAlpha);
    drawVector(originX, originY, b_endX, b_endY);

    // 3. Draw the Angle Arc for α
    const angleArcRadius = 40;
    // In canvas coordinates, the vertical downward direction is at an angle of Math.PI / 2.
    // The arc starts from e_n and sweeps by angleAlpha towards B.
    ctx.beginPath();
    ctx.arc(originX, originY, angleArcRadius, Math.PI / 2, Math.PI / 2 + angleAlpha);
    ctx.stroke();

    // 4. Add Labels
    // Label 'S' for the curved surface
    ctx.font = 'italic 28px Times New Roman';
    ctx.fillText('S', cx + 90, cy - 85);

    // Label 'S'' for the flat base
    ctx.fillText("S'", cx + 95, cy - 10);

    // Label 'α' for the angle
    ctx.font = 'italic 24px Times New Roman';
    ctx.fillText('α', cx + 25, cy + 45);
    
    // Label 'B' for the magnetic field vector
    ctx.font = 'bold italic 28px Times New Roman';
    ctx.fillText('B', b_endX + 25, b_endY + 5);

    // Label 'e_n' for the normal vector (with subscript)
    ctx.font = 'italic 28px Times New Roman';
    ctx.fillText('e', en_endX - 30, en_endY);
    ctx.font = 'italic 20px Times New Roman';
    ctx.fillText('n', en_endX - 19, en_endY + 8);

    // 5. Add Caption
    // Use a font that supports Chinese characters
    ctx.font = '22px "Songti SC", "SimSun", serif';
    ctx.fillText('题 7 图', cx, canvas.height - 30);

</script>

</body>
</html>